<?php
namespace app\erp\model;

use think\Model;
use traits\model\SoftDelete;
use app\erp\validate\Judgerule;
use think\Cache;

class Judgedb extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    protected $pk = 'judge_id';

    protected $name = 'judge';

    protected $type = [
        'create_time' => 'timestamp:Y-m-d H:i',
        'delete_time' => 'timestamp:Y-m-d',
        'replay_time' => 'timestamp:Y-m-d H:i'
    ];
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;

    protected $insert = [
        'partner_id' => PARTNER_ID
    ];

    public $param = array();

    protected function cache_name($name)
    {
        $new_arr = $this->param;
        $new_arr['partner_id'] = PARTNER_ID;
        if (! isset($new_arr['site_id'])) {
            $new_arr['site_id'] = SITE_ID;
        }
        ksort($new_arr);
        $param_str = http_build_query($new_arr);
        return md5($name . $param_str);
    }

    public function scopeMemberId($query, $member_id)
    {
        $query->where('member_id', $member_id);
    }

    public function scopeGoodsSku($query, $goods_sku)
    {
        $query->where('goods_sku', $goods_sku);
    }

    public function scopeCartId($query, $cart_id)
    {
        $query->where('cart_id', $cart_id);
    }

    public function scopeJudgeStar($query, $judge_star)
    {
        $query->where('judge_star', 'IN', $judge_star);
    }
    // 基础查询
    public function base($query)
    {
        $field = $this->getDeleteTimeField(true);
        if ($field) {
            if (isset($this->param['delete']) && $this->param['delete'] == 'yes') {
                $this->onlyTrashed();
            } else {
                $query->useSoftDelete($field);
            }
        }
        $query->where('partner_id', PARTNER_ID);
        if (isset($this->param['member_id']) && $this->param['member_id']) {
            $query->memberId($this->param['member_id']);
        }
        if (isset($this->param['goods_sku']) && $this->param['goods_sku']) {
            $query->goodsSku($this->param['goods_sku']);
        }
        if (isset($this->param['cart_id']) && $this->param['cart_id']) {
            $query->cartId($this->param['cart_id']);
        }
        if (isset($this->param['judge_star']) && $this->param['judge_star']) {
            $query->judgeStar($this->param['judge_star']);
        }
    }

    public function ajaxSave()
    {
        $rule = new Judgerule();
        $this->param['partner_id'] = PARTNER_ID;
        if (isset($this->param['judge_id']) && $this->param['judge_id'] != '' && $this->param['judge_id'] != 0) {
            unset($this->param['partner_id']);
            $this->save($this->param, [
                'judge_id' => $this->param['judge_id']
            ]);
        } else {
            if (! $rule->check($this->param)) {
                return $rule->getError();
            }
            unset($this->param['partner_id']);
            $this->allowField(true)->save($this->param);
            $cartdb = new Cartdb();
            $cartdb->param['cart_id'] = $this->param['cart_id'];
            $get_cart = $cartdb->ajaxFind();
            if ($get_cart) {
                $orderdb = new Orderdb();
                $orderdb->param['order_no'] = $get_cart['order_no'];
                $orderdb->param['event'] = 'add_judge';
                $orderdb->param['cart_ids'] = $this->param['cart_id'];
                $orderdb->param['member_id'] = $this->param['member_id'];
                $orderdb->editOrder();
            }
        }
        Cache::clear('judge');
        return 'success';
    }

    public function ajaxSelect()
    {
        $data['data']['list'] = $this->ajaxList();
        $data['data']['sum'] = $this->ajaxSum();
        $data['count'] = $this->ajaxCount();
        return $data;
    }

    public function ajaxSum()
    {
        // 所有
        $data['all'] = $this->useGlobalScope(false)
            ->where('goods_sku', $this->param['goods_sku'])
            ->count();
        $data['bad'] = $this->useGlobalScope(false)
            ->where('goods_sku', $this->param['goods_sku'])
            ->where('judge_star', 'IN', '1,2')
            ->count();
        $data['just'] = $this->useGlobalScope(false)
            ->where('goods_sku', $this->param['goods_sku'])
            ->where('judge_star', 3)
            ->count();
        $data['good'] = $this->useGlobalScope(false)
            ->where('goods_sku', $this->param['goods_sku'])
            ->where('judge_star', 'IN', '4,5')
            ->count();
        if ($data['all'] == 0) {
            $data['bad_rate'] = 0;
            $data['just_rate'] = 0;
            $data['good_rate'] = 100;
        } else {
            $data['bad_rate'] = round($data['bad'] / $data['all'] * 100);
            $data['just_rate'] = round($data['just'] / $data['all'] * 100);
            $data['good_rate'] = round($data['good'] / $data['all'] * 100);
        }
        return $data;
    }

    public function ajaxCount()
    {
        $data = $this->cache($this->cache_name('judge_count'), 86400, 'judge')
            ->count();
        return $data;
    }

    public function ajaxList()
    {
        $data = $this->page($this->param['page'], $this->param['limit'])
            ->cache($this->cache_name('judge_list'), 86400, 'judge')
            ->order('judge_id desc')
            ->select();
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $key => $val) {
                $data[$key] = $this->handleData($val);
            }
            return $data;
        } else {
            return array();
        }
    }
    // 数据处理
    protected function handleData($val)
    {
        foreach ($val as $k => $v) {
            $val[$k] = $v;
            switch ($k) {
                case 'member_id':
                    $memberdb = new Memberdb();
                    $val['member_info'] = $memberdb->memberInfo($val['member_id']);
                    break;
            }
        }
        return $val;
    }
}